$(document).ready(function() {
	if(null != id && "" != id){
		initUpdateCompany(id);
	}else{
		getNewCode();
		loadeResource();
		if(flag == ''){
			queryCompanys("",0,"");
		}
	}
});
var pageSize = 5;
var pageNo = 0;
//通过用户名查询用户
function selCompanyByName(){
	queryCompanys("",0,$("#resourcecode").val());
}
var queryCompanys = function(condition,pageNo,companyName){
	$("#jigou").hide();//影藏当前页面数据
	$("#loadingtable").show();//数据加载进度提示
	jQuery.post("/finance/company/selCompanys.do",{pageSize:pageSize,pageNo:pageNo,companyName:companyName},function(str){
			$("#loadingtable").hide();//隐藏数据加载进度提示
			$("#jigou").show();//影藏当前页面数据
			//拼表头
			var htmltxtHead = "<caption>公司信息</caption>"
								+'<thead>'
								+'<tr>'
								+'<th width="50">序号</th>'
								+'<th width="75">公司代码</th>'
								+'<th width="70">公司名称</th>'
								+'<th width="100">创建时间</th>'
								+'<th width="70">是否有效</th>'
								+'<th width="175">操作</th>'
								+'</tr>'
								+'</thead>'
			var htmltxtcontent = "";	
			var data = eval('(' + str + ')');
			htmltxtcontent += '<tbody>';
			if(data.data.length>0){
				for (var i=0; i<data.data.length; i++) {
				 htmltxtcontent += '<tr>'
				 				   +'<td width="50" align="center">'+ (i+1) +'</td>'
				 				   +'<td width="75" align="center">'+ data.data[i].companyCode +'</td>'
				 				   +'<td width="70" align="center">'+ data.data[i].companyName +'</th>'
				 				   +'<td width="100">'+ data.data[i].createTime +'</th>'
				 				   +'<td width="70">'+ (data.data[i].isActive==1?"有效":"无效") +'</th>'
				 				   +'<td width="175"><div align="center">'
								   +'<a href="javascript:toupdate('+data.data[i].id+',\''+data.data[i].companyCode+'\');" target="_self">修改</a>|'
								   +'<a href="javascript:delCompany('+data.data[i].id+',\''+data.data[i].companyCode+'\');" target="_self">删除</a>'
				 				   +'</div></th>'
						         +'</tr>';
				}
			}else{
				htmltxtcontent += '<tr>'
								+'<td width="50" colspan="6" align="center">对不起,没有您要查询的信息!</td>'
								+'</tr>';
			}
			
			htmltxtcontent += '</tbody>';
			 $("#jigou").html(htmltxtHead+htmltxtcontent);
			  //分页器
			if(data.totalPageCount > 1){
				$("#pagingToolDiv").show();
	            $("#pagingToolDiv").html(PagingToolV2.getPagingCode('bottomgo',condition, 'queryCompanys', data.totalPageCount,data.totalcount, pageNo));
            	$("#bottomgo").keydown(function(event){
                     if(event.keyCode==13){
                       if(parseInt($("#bottomgo").val())!=NaN){
                       	 if(data.totalPageCount<parseInt($("#bottomgo").val())){
                       	   parseInt($("#bottomgo").val(data.totalPageCount))
                       	 }
                         queryCompanys('',parseInt($("#bottomgo").val()));
                       }
                     }
                });
            }else{
            	$("#pagingToolDiv").hide();
            }
        });
        
}
//删除公司
function delCompany(id,companyCode){
	if (confirm("确认删除?该操作会删除与该公司相关所有数据!!!")){
		jQuery.post("/finance/company/delCompany.do",{id:id,companyCode:companyCode},function(str){
			var data = eval('(' + str + ')');
			if(data.success){
				jAlert("删除公司信息成功!","提示");
				queryCompanys("",0,"");
			}else{
				jAlert("删除公司信息失败!","提示");			
			}
	    });
	 }else{
	 	return;
	 }
}

//修改公司
function updateCompany(){
	var id = $("#companyId").val();
	var code = $("#companyCode").val();
	var name = $("#companyName").val();
	var resourceids="";
	$(":checkbox[name='resouceschecked']").each(function (i,v){
		if($(v).attr("checked") =="checked"){
			resourceids= resourceids +$(v).val()+",";
		}
	});
	jQuery.post(webContext + "/company/updateCompany.do",{id:id,companyCode:code,companyName:name,isActive:1,resourceids:resourceids},function(str){
		var data = eval('(' + str + ')');
		if(data.success){
			jAlert("修改公司信息成功!","提示");
		}else{
			jAlert("修改公司信息失败!","提示");			
		}
    });
}

//跳转到新增用户页面
function toAddCompanyPage(){
	window.open(webContext + "/views/console/addCompany.jsp?flag=1","_self");
}

function toupdate(id){
	window.open(webContext + "/views/console/updateCompany.jsp?id="+id,"_self");
}

function loadeResource(){
	jQuery.post(webContext + "/resources/selResources.do",{pageSize:100,pageNo:0},function(str){
		var data = eval('(' + str + ')');
		if(data.success){
		var html = "";
			for(var i=0; i<data.data.length; i++){
				html+=""+(i%5==0?"<br/>":"")+"<input type=\"checkbox\" name=\"resouceschecked\" value=\""+data.data[i].id+"\"/>"+data.data[i].ResourceCode+" ";
			}
			$("#showResource").html(html);
		}else{
			jAlert("加载公司列表失败,请联系管理员!","提示");			
		}
    });
}

//新增公司
function addCompany(){
	var code = $("#companyCode").val();
	var name = $("#companyName").val();
	if($("#companyCode").val()=="" ){
		jAlert("公司code不能为空！","提示");
		return false;
	}
	if($("#companyName").val()=="" ){
		jAlert("公司名称不能为空！","提示");
		return false;
	}
	
	var resourceids="";
	$(":checkbox[name='resouceschecked']").each(function (i,v){
		if($(v).attr("checked") =="checked"){
			resourceids= resourceids +$(v).val()+",";
		}
	});
	resourceids = resourceids.substring(0,resourceids.length-1);
	jQuery.post(webContext + "/company/addCompany.do",{companyCode:code,companyName:name,resourceids:resourceids},function(str){
		var data = eval('(' + str + ')');
		if(data.success){
			jAlert("新增公司信息成功!","提示");
			formReset();//重置
		}else{
			jAlert("新增公司信息失败!","提示");			
		}
    });
}

//配置公司资源
function addCompanyResources(){
	jQuery.post(webContext + "/company/addCompanyResources.do",{companyId:"2",resourceIdStr:"2,3,4,5"},function(str){
		var data = eval('(' + str + ')');
		if(data.success){
			jAlert("配置公司资源成功!","提示");
			window.open("/finance/views/console/addCompany.jsp","_self");
		}else{
			jAlert("配置公司资源失败!","提示");			
		}
    });
}

function initUpdateCompany(ccode){

	$.ajax({  
          type : "post",  
          url : webContext + "/resources/selResources.do",  
          async : false,  
          data:{
          		pageSize:100,
          		pageNo:0
          },
          success :function(str1){
			var data1 = eval('(' + str1 + ')');
			if(data1.success){
			var html = "";
			for(var i=0; i<data1.data.length; i++){
				html+=""+(i%5==0?"<br/>":"")+"<input type=\"checkbox\" name=\"resouceschecked\" value=\""+data1.data[i].id+"\"/>"+data1.data[i].ResourceCode+" ";
			}
			}else{
				jAlert("加载公司列表失败,请联系管理员!","提示");			
			}
			$("#showResource").html(html);
		   }
        });  
	
	jQuery.post(webContext + "/company/initUpdateCompany.do",
		{id:id},function(str){
			var data = eval('(' + str + ')');
			$("#companyId").val(data.id);
			$("#companyCode").val(data.companyCode);
			$("#companyName").val(data.companyName);
			var crs = data.crs;
			$(":checkbox[name='resouceschecked']").each(function (i,v){
				for(var a=0 ;a<crs.length;a++){
					if($(v).val() ==crs[a].resourceId ){
						$(v).attr("checked","checked");
					}
				}
			});
	      });
}

//重置
function formReset(){
	$("#companyCode").val("");
	$("#companyName").val("");
	$(":checkbox[name='resouceschecked']").each(function (i,v){
			$(v).attr("checked",false);
	});
}

//获取公司新Code
function getNewCode(){
	jQuery.post("/finance/company/newCode.do",
	{code:"C"},function(str){
		var data = eval('(' + str + ')');
		code = data.code;
		$("#companyCode").val(code);
      });
}
